/*
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.vstore.activities;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

import com.appvn2014.appstorevn.DeveloperKey;
import com.appvn2014.appstorevn.R;
import com.appvn2014.appstorevn.R.id;
import com.appvn2014.appstorevn.R.layout;
import com.flurry.android.FlurryAgent;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import anroid.vstore.provider.Constant;

/**
 * A sample showing how to use the ActionBar as an overlay when the video is playing in fullscreen.
 *
 * The ActionBar is the only view allowed to overlay the player, so it is a useful place to put
 * custom application controls when the video is in fullscreen. The ActionBar can not change back
 * and forth between normal mode and overlay mode, so to make sure our application's content
 * is not covered by the ActionBar we want to pad our root view when we are not in fullscreen.
 */
@TargetApi(11)
public class YoutubePlayerActivity extends YouTubeFailureRecoveryActivity implements
YouTubePlayer.OnFullscreenListener {

	private ActionBarPaddedFrameLayout viewContainer;
	private YouTubePlayerFragment playerFragment;
	private ActionBar actionBar;
	String vid_id;
	String vid_name;
	YouTubePlayer player;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_youtube_player);

		viewContainer = (ActionBarPaddedFrameLayout) findViewById(R.id.view_container);
		playerFragment =
				(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.player_fragment);
		playerFragment.initialize(Constant.YOUTUBE_DEVELOPER_KEY, this);
		viewContainer.setActionBar(getActionBar());


		//
		vid_id = getIntent().getExtras().getString("videoID");
		vid_name = getIntent().getExtras().getString("videoName");
		// Action bar background is transparent by default.
		InitActionBar();


	}
	@SuppressLint("NewApi")
	protected void InitActionBar() {
		if(android.os.Build.VERSION.SDK_INT >= 11){
			// get the action bar
			actionBar = getActionBar();
			actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.main)));
			// Enabling Back navigation on Action Bar icon
			actionBar.setDisplayHomeAsUpEnabled(true);
			actionBar.setNavigationMode(ActionBar.DISPLAY_HOME_AS_UP);
			actionBar.setTitle(vid_name);
		}
	}
	@Override
	public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
			boolean wasRestored) {
		this.player = player;
		player.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
		player.setOnFullscreenListener(this);
		
		if (!wasRestored) {
			player.cueVideo(vid_id);
		}
	}

	@Override
	protected YouTubePlayer.Provider getYouTubePlayerProvider() {
		return (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.player_fragment);
	}

	@Override
	public void onFullscreen(boolean fullscreen) {
		viewContainer.setEnablePadding(!fullscreen);

		ViewGroup.LayoutParams playerParams = playerFragment.getView().getLayoutParams();
		if (fullscreen) {
			playerParams.width = MATCH_PARENT;
			playerParams.height = MATCH_PARENT;
		} else {
			playerParams.width = WRAP_CONTENT;
			playerParams.height = WRAP_CONTENT;
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.scene_detail_item, menu);
		return true;
	}
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == android.R.id.home) {
			this.finish();
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	/**
	 * This is a FrameLayout which adds top-padding equal to the height of the ActionBar unless
	 * disabled by {@link #setEnablePadding(boolean)}.
	 */
	public static final class ActionBarPaddedFrameLayout extends FrameLayout {

		private ActionBar actionBar;
		private boolean paddingEnabled;

		public ActionBarPaddedFrameLayout(Context context) {
			this(context, null);
		}

		public ActionBarPaddedFrameLayout(Context context, AttributeSet attrs) {
			this(context, attrs, 0);
		}

		public ActionBarPaddedFrameLayout(Context context, AttributeSet attrs, int defStyle) {
			super(context, attrs, defStyle);
			paddingEnabled = true;
		}

		public void setActionBar(ActionBar actionBar) {
			this.actionBar = actionBar;
			requestLayout();
		}

		public void setEnablePadding(boolean enable) {
			paddingEnabled = enable;
			requestLayout();
		}

		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			int topPadding =
					paddingEnabled && actionBar != null && actionBar.isShowing() ? actionBar.getHeight() : 0;
					setPadding(0, topPadding, 0, 0);

					super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		FlurryAgent.onStartSession(this, "9J4G5CDTVJ76YCNF797C");
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		FlurryAgent.onEndSession(this);
	}
}
